package com.neuedu.nep.service.impl;

import com.neuedu.nep.dao.AqiFeedbackDao;
import com.neuedu.nep.dao.StatisticDao;
import com.neuedu.nep.model.AqiFeedback;
import com.neuedu.nep.model.Statistic;
import com.neuedu.nep.service.StatisticService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 黄宇佳
 * @create 2024-04-11 16:14
 * @description:
 */
@Service
public class StatisticServiceImpl implements StatisticService {
    private static final Object LOCKED = new Integer(0);
    @Resource
    private AqiFeedbackDao aqiFeedbackDao;

    @Resource
    private StatisticDao statisticDao;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateStatistic(Statistic statistic) {
        AqiFeedback aqiFeedback=new AqiFeedback();
        aqiFeedback.setAfId(statistic.getId());
        aqiFeedback.setState(2);
        int result = 0;
        synchronized (LOCKED){
        result+=aqiFeedbackDao.updateByPrimaryKeySelective(aqiFeedback);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format=simpleDateFormat.format(new Date());
        String[] s=format.split(" ");
        String Date=s[0];
        String Time=s[1];
        statistic.setConfirmDate(Date);
        statistic.setConfirmTime(Time);
        result+=statisticDao.updateByPrimaryKeySelective(statistic);
        }
        return result;
    }
}
